﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using SMSSnew.DATA;
using SMSSnew.CLASSES;
using System.IO;

using System.Collections.Generic;
using System.Web.Services;
using System.Text.RegularExpressions;
using System.Collections;
using System.Drawing;

namespace SMSSnew.BIZ
{
    public class HomeBIZ
    {
        DataTable dt;
        HomeDA home;

        //handle business logic for login
        public bool login(userCLZ objUser)
        {
            home = new HomeDA();
            dt = home.login(objUser);
            int noOfRows = dt.Rows.Count;
            bool verify = false;

            if (noOfRows != 0)
            {
                verify = true;
            }

            return verify;
        }

        //handle business logic for getting user type to direct user to appropriate next web page
        public string getUserData(userCLZ objUser)
        {
            home = new HomeDA();
            string userType, fullName="";
            byte[] userImage = new byte[0];
            string imageURL = "~/IMAGES/profile-default.png";
            string data;

            dt = home.getUserData(objUser);

            userType = dt.Rows[0][0].ToString();
            fullName = dt.Rows[0][1].ToString();

            if (DBNull.Value != dt.Rows[0][2])
            {
                userImage = (byte[])dt.Rows[0][2];
                System.Drawing.Image photo = byteArrayToImage(userImage);

                photo.Save(HttpContext.Current.Server.MapPath("~/EmpImages") + "\\" + objUser.user_name + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                imageURL = "~/EmpImages/" + objUser.user_name + ".jpg";
            }

            data = userType + "|" + fullName + "|" + imageURL;
            return data;
        }


        public System.Drawing.Image byteArrayToImage(byte[] byteArrayIn)
        {
            MemoryStream ms = new MemoryStream(byteArrayIn);
            System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);
            return returnImage;
        }


        //handle business logic for loading security question and answer when he click forgot the password
        public string loadSecData(userCLZ objUser, string type)
        {
            home = new HomeDA();
            string data = "";
            dt = home.getSecQues(objUser);

            if (type == "Question")
                data = dt.Rows[0][0].ToString();
            else if(type == "Answer")
                data = dt.Rows[0][1].ToString();

            return data;
        }

        //handle business logic for sending mail to user
        public bool fillAndSendMail(userCLZ objUser)
        {
            home = new HomeDA();
            bool message = false;
            mailsCLZ myMail = new mailsCLZ();
            myMail.subject = "Your Password";

            dt = home.loadReceiverMailAndPassword(objUser);

            myMail.receiverEmail = dt.Rows[0][0].ToString();
            myMail.message = dt.Rows[0][1].ToString();

            message = myMail.SendMail(myMail);
            return message;
        }

       
    }
}
